package lib.listen;

import lib.Dao.*;
import lib.Model.Course;
import lib.Model.School;
import lib.servlet.AuthUtil;
import lib.util.PostMessage;
import net.sf.json.JSONObject;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

public class MonitorCouserNum extends TimerTask{

    @Override
    public void run() {
        CourseDAO courseDAO=new CourseDAO();
        List<Course> courseList=new ArrayList<>();
        CourseUserDAO courseUserDAO=new CourseUserDAO();
        PostMessage postMessage=new PostMessage();
        List<String> list=new ArrayList<>();
        SchoolDAO schoolDAO=new SchoolDAO();
        Statement stmt = null;
        Dbutil dbutil = new Dbutil();
        Connection con = null;
        ResultSet rs = null;


        try{
            con = dbutil.getCon();
            stmt = con.createStatement();
            String sql = "select * from school_list;";
            rs = stmt.executeQuery(sql);
            while(rs.next()) {
                String se=rs.getString("se");
                try {
                    courseList=courseDAO.getMonitorCourse(se);
                    for(int i=0;i<courseList.size();i++){
                        int rongliang=courseList.get(i).getRongliang();
                        int cnum=courseList.get(i).getCnum();
                        int isposted=courseList.get(i).getIsposted();

                        if(rongliang>cnum && isposted==0){
                            courseDAO.SetIsposted(se,courseList.get(i).getCid(),1);
                            String coursedata="课程名称："+courseList.get(i).getCname()+"   授课老师："+courseList.get(i).getCteacher()+"   上课时间："+courseList.get(i).getCtime();
                            list=courseUserDAO.getCourseUser(courseList.get(i).getCid());
                            for(int j=0;j<list.size();j++){

                                postMessage.posttoopenid(list.get(j).replace("\n",""),"课程出现余量",coursedata);
                            }
                        }

                        if(rongliang<=cnum && isposted==1){
                            courseDAO.SetIsposted(se,courseList.get(i).getCid(),0);
                            String coursedata="课程名称："+courseList.get(i).getCname()+"   授课老师："+courseList.get(i).getCteacher()+"   上课时间："+courseList.get(i).getCtime();
                            list=courseUserDAO.getCourseUser(courseList.get(i).getCid());
                            for(int j=0;j<list.size();j++){
                                postMessage.posttoopenid(list.get(j).replace("\n",""),"课程余量已消失",coursedata);
                            }
                        }

                    }

                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }





        JSONObject jsonObject;

    }
}